<?php

require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';

?>

<div id="subcontent">
<h4>View FT stats by season - Cumulative</h4>


<form>
	
<select class="form-control"  name="season" id="select1">
		
<?php

if (isset($_POST['season'])) {

	$setseason = $_POST['season'];

echo  "<option selected=\"selected\" value=\"$setseason\"> $setseason is selected</option>";
	
} else {
	echo  "<option selected=\"selected\" disabled=\"disabled\"> Select season</option>";
}


$sql = "SELECT DISTINCT season 
FROM trainingft";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
	$season = $row['season'];

	echo "<option value=\"$season\"> $season </option>";
}

?>
</select>

</form>

<?php

if (isset($_POST['season'])) {
	
	$setseason = $_POST['season'];

	$sql2 = "SELECT trainingft.playerID, SUM(trainingft.ftscored) 
	AS ftscored, SUM(trainingft.fttotal) 
	AS fttotal, users.firstname, users.lastname
	FROM trainingft
	JOIN users
	ON users.user_key = trainingft.playerID
	WHERE trainingft.season = '$setseason'
	AND trainingft.playerID IN (SELECT playerID FROM teams WHERE season = '$setseason' AND playertype = 'player')
	AND users.type = 'Player'
	GROUP BY trainingft.playerID
	ORDER BY users.playernumber";

	$result2 = $db->query($sql2);

?>
<style type="text/css">
	
@media print {
.stats {
        background-color: white;
        height: 100%;
        width: 100%;
        position: fixed;
        top: 0;
        left: 0;
        margin: 0;
        padding: 15px;
        font-size: 14px;
        line-height: 18px;
    }
}

</style>

<br>
<div class="table-responsive stats">
<h4>Cumulative FT stats of <?= $setseason; ?></h4>

<p>
<button onclick="myFunction()" class="btn btn-primary btn-sm hidden-print">Print FT Cumu Stats</button>
</p>

<table class="table table-hover table-stripe" id="sorttable">
    <thead>
      <tr>
        <th>#</th>
        <th>Name</th>
        <th>FTs</th>
        <th>Percentage</th>
      </tr>
    </thead>
    <tbody>
<?php
	while ($row2 = $result2->fetch_assoc()) {

		$firstname = $row2['firstname'];
		$lastname = $row2['lastname'];		
		$ftscored = $row2['ftscored'];
		$fttotal = $row2['fttotal'];
		$playerID = $row2['playerID'];

		$sql = "SELECT playernumber FROM teams WHERE playerID = '$playerID' AND season = '$setseason'"; 

		$result3 = $db->query($sql);
		$row3 = $result3->fetch_assoc();
		$playernumber = $row3['playernumber'];


		if ($fttotal == 0){
			$ftperc = 0;
		} else {

			$ftperc = round($ftscored/$fttotal*100);
		}

		echo "
		<tr>
		<td> $playernumber </td>
		<td>$firstname $lastname</td>
		<td>$ftscored/$fttotal</td>
		<td>$ftperc%</td>
		</tr>
		";

}

?>


    </tbody>
 </table>




</div>

<?php } ?>
</div>




<script type="text/javascript">
	
	$("#select1").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./training_viewftstatscumu.php",
        data: postData,
        success: function(data) { 
            $("#subcontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#subcontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

function myFunction() {
    window.print();
}
	
$(document).ready(function() {
    $('#sorttable').DataTable({
    	"paging":   false,
    	"searching": false,
    	"info": false
    });
});

</script>